package com.ly.mpayment.persist.service.platformaccountrecord;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.PaginationUtil;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.rest.system.BigDecimalUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.PlatFormAccountRecord;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
@Service
public class PlatFormAccountRecordServiceImpl extends AbstractService<PlatFormAccountRecord, Serializable> implements IPlatFormAccountRecordService {

	@Override
	public boolean addPlatformaccountrecord(PlatFormAccountRecord plat) {
		if(plat != null){
			this.saveOrUpdate(plat);
			return true;
		}
		return false;
	}

	@Override
	public boolean updatePlatformaccountrecord(PlatFormAccountRecord plat) {
		if(plat != null){
			this.saveOrUpdate(plat);
			return true;
		}
		return false;
	}

	@Override
	public PlatFormAccountRecord findById(String id) {
		return this.findUnique("select p from PlatFormAccountRecord p where p.changeuuid = ?", id);
	}

	@Override
	public Page<PlatFormAccountRecord> findPagePlatformaccountrecord(Map<String, Object> parameters, int start,
			int pageSize) {
		String hql = "select p from PlatFormAccountRecord p left join fetch p.user u where 1 = 1 #[ and u.username = :name]#   ";
		if(parameters.containsKey(":date")){
			hql+= " and DATE_FORMAT(p.changetime,'%Y-%m-%d') = '"+parameters.get(":date").toString().concat("'");
			parameters.remove(":date");
		}
		hql+=" order by p.changetime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}


	@Override
	public String getProductDiscount(String data) {
		JSONObject jsonResult = JSONObject.fromObject(data);
		String sql = " select DISTINCT d.discount,u.username,ds.downfacevalue, ud.discount '上游折扣',up.username 'upname' " +
				" from down_order d inner join downshopproduct ds on ds.downprouuid = d.downprouuid " +
				" inner join user u on u.useruuid = ds.useruuid " +
				" inner join uporder ud on ud.downorderuuid = d.downorderuuid " +
				" inner join user up on up.useruuid = ud.useruuid where 1 = 1 ";
		if(jsonResult.containsKey("startTime") && !StringUtils.isBlank(jsonResult.getString("startTime"))){
			sql += " and  d.createtime >='"+ jsonResult.getString("startTime")+"' ";
		}
		if(jsonResult.containsKey("endTime") && !StringUtils.isBlank(jsonResult.getString("endTime"))){
			sql += " and  d.createtime <='"+ jsonResult.getString("endTime")+"' ";
		}

		if(jsonResult.containsKey("upId") && !StringUtils.isBlank(jsonResult.getString("upId"))){
			sql += " and ud.useruuid = '"+ jsonResult.getString("upId")+"' ";
		}

		if(jsonResult.containsKey("downId") && !StringUtils.isBlank(jsonResult.getString("downId"))){
			sql += " and d.useruuid = '"+ jsonResult.getString("downId")+"' ";
		}
		 sql += "  order by u.username ,upname ";

		Page pa = new Page();
		pa.setStart(jsonResult.getInt("start"));
		pa.setPageSize(jsonResult.getInt("pageSize"));
		Page page = PaginationUtil.queryPaginationSQL( "select * from ("+ sql +") dfs", pa,
				this.getSession());
		List lists = page.getResult();
		if (lists != null && !lists.isEmpty()) {
			JSONArray array = JsonUtils.getJSONARRAY();
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] object = (Object[]) lists.get(i);
				//d.discount,u.username,ds.downfacevalue, ud.discount '上游折扣',up.username 'upname'
				obj.put("discountDown", object[0] == null ? "" : object[0].toString());
				obj.put("username", object[1] == null ? "" : object[1].toString());
				obj.put("downfacevalue",  object[2] == null ? 0 : Integer.valueOf(object[2].toString()) );
				obj.put("discountUp", object[3] == null ? BigDecimal.ZERO : new BigDecimal(object[3].toString()));
				obj.put("upname", object[4] == null ? BigDecimal.ZERO : object[4].toString());
				array.add(obj);
			}
			page.setResult(array);
			return JsonUtils.convertObjectToJson(page);
		}
		return JsonUtils.getJSONARRAY().toString();
	}
}
